package com.easemob.chat;

import android.content.Context;
import android.content.Intent;
import com.easemob.EMNotifierEvent.Event;
import com.easemob.util.EMLog;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

class EMOfflineMessageHandler
{
  private static final String TAG = "EMOfflineMessageHandler";
  Set<String> offlineMsgUserSenders = Collections.synchronizedSet(new HashSet());
  Set<String> offlineMsgGrpSenders = Collections.synchronizedSet(new HashSet());
  List<EMMessage> offlineMessages = new ArrayList();
  private EMMessage curOfflineMsg;
  private EMMessage preOfflineMsg;
  private static final long OFFLINE_INTERVAL = 3000L;
  private Thread notifyThread = null;
  private boolean needSend = false;

  void onAppReady()
  {
    this.needSend = true;
  }

  void onNewOfflineMessage(EMMessage paramEMMessage)
  {
    if (!this.offlineMessages.contains(paramEMMessage))
      this.offlineMessages.add(paramEMMessage);
    this.curOfflineMsg = paramEMMessage;
    startOfflineThread();
    EMLog.d("EMOfflineMessageHandler", " offline msg, do not send notify for msg:" + paramEMMessage.msgId);
    String str;
    if (paramEMMessage.getChatType() == EMMessage.ChatType.GroupChat)
    {
      str = paramEMMessage.getTo();
      EMLog.d("notify", "offline group msg");
      if (!this.offlineMsgGrpSenders.contains(str))
        this.offlineMsgGrpSenders.add(str);
    }
    else
    {
      str = paramEMMessage.getFrom();
      if (!this.offlineMsgUserSenders.contains(str))
        this.offlineMsgUserSenders.add(str);
    }
  }

  void stop()
  {
    sendOfflineBroadcast();
    stopOfflineThread();
  }

  synchronized void startOfflineThread()
  {
    if ((this.notifyThread != null) && (this.notifyThread.isAlive()))
      return;
    this.notifyThread = new Thread()
    {
      public void run()
      {
        EMLog.d("EMOfflineMessageHandler", "start offline notify thread...");
        long l = 3L;
        try
        {
          while (true)
          {
            sleep(1000L);
            l -= 1L;
            if (l <= 0L)
            {
              l = 3L;
              if ((EMOfflineMessageHandler.this.curOfflineMsg == EMOfflineMessageHandler.this.preOfflineMsg) && (EMOfflineMessageHandler.this.needSend))
              {
                if (EMOfflineMessageHandler.this.curOfflineMsg != null)
                {
                  EMOfflineMessageHandler.this.sendOfflineBroadcast();
                  EMOfflineMessageHandler.this.notifyThread = null;
                  EMOfflineMessageHandler.this.curOfflineMsg = null;
                  EMOfflineMessageHandler.this.preOfflineMsg = null;
                }
                return;
              }
              EMOfflineMessageHandler.this.preOfflineMsg = EMOfflineMessageHandler.this.curOfflineMsg;
              if (EMOfflineMessageHandler.this.needSend)
                EMOfflineMessageHandler.this.sendOfflineBroadcast();
            }
          }
        }
        catch (InterruptedException localInterruptedException)
        {
          EMOfflineMessageHandler.this.notifyThread = null;
          EMOfflineMessageHandler.this.curOfflineMsg = null;
          EMOfflineMessageHandler.this.preOfflineMsg = null;
        }
      }
    };
    this.notifyThread.start();
  }

  void stopOfflineThread()
  {
    if (this.notifyThread != null)
      this.notifyThread.interrupt();
  }

  private synchronized void sendOfflineBroadcast()
  {
    Object localObject;
    if ((this.offlineMsgUserSenders.size() > 0) || (this.offlineMsgGrpSenders.size() > 0))
    {
      localObject = new Intent(EMChatManager.getInstance().getOfflineMessageBroadcastAction());
      String[] arrayOfString1 = (String[])this.offlineMsgUserSenders.toArray(new String[0]);
      ((Intent)localObject).putExtra("fromuser", arrayOfString1);
      EMLog.d("EMOfflineMessageHandler", "send offline message broadcast for users:" + arrayOfString1.length);
      String[] arrayOfString2 = (String[])this.offlineMsgGrpSenders.toArray(new String[0]);
      ((Intent)localObject).putExtra("fromgroup", arrayOfString2);
      EMLog.d("EMOfflineMessageHandler", "send offline message broadcast for groups:" + arrayOfString2.length);
      try
      {
        Context localContext = EMChatConfig.getInstance().getApplicationContext();
        if ((localContext != null) && (this.curOfflineMsg != null))
        {
          localContext.sendOrderedBroadcast((Intent)localObject, null);
          EMChatManager.getInstance().notifyMessage(this.curOfflineMsg);
        }
      }
      catch (Exception localException)
      {
        localException.printStackTrace();
      }
      this.offlineMsgUserSenders.clear();
      this.offlineMsgGrpSenders.clear();
    }
    if (this.offlineMessages.size() > 0)
    {
      localObject = new ArrayList();
      ((List)localObject).addAll(this.offlineMessages);
      EMNotifier.getInstance(EMChat.getInstance().getAppContext()).publishEvent(EMNotifierEvent.Event.EventOfflineMessage, localObject);
      this.offlineMessages.clear();
    }
  }

  void reset()
  {
    this.curOfflineMsg = null;
    this.preOfflineMsg = null;
    if (this.notifyThread != null)
      this.notifyThread.interrupt();
    this.offlineMsgUserSenders.clear();
    this.offlineMsgGrpSenders.clear();
    this.notifyThread = null;
    this.offlineMessages.clear();
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     com.easemob.chat.EMOfflineMessageHandler
 * JD-Core Version:    0.6.1
 */